package richbar.com.richbar.activity.login;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.common.base.BaseActivity;
import com.common.util.LogUtil;
import com.common.util.StringUtil;
import com.common.util.ToastUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import richbar.com.richbar.R;
import richbar.com.richbar.bean.CodeBean;
import richbar.com.richbar.bean.LoginBean;
import richbar.com.richbar.help.CountDownTimerHelp;
import richbar.com.richbar.help.TitleHelp;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;

/**
 * Created by QiYang on 2018/10/16.
 * 注册
 */

public class RegisterAc extends BaseActivity {
    @BindView(R.id.tel)
    EditText tel;
    @BindView(R.id.code)
    EditText code;
    @BindView(R.id.get_code)
    TextView get_code;
    @BindView(R.id.pwd)
    EditText pwd;
    @BindView(R.id.pwd_tip)
    TextView pwd_tip;
    @BindView(R.id.pwd_confirm)
    EditText pwdConfirm;
    @BindView(R.id.complete)
    TextView complete;
    private LoadingDialog dialog;

    @Override
    public int initLayout() {
        return R.layout.ac_register_and_forget_pwd;
    }

    @Override
    public void initTitle() {
        new TitleHelp(this).setTitle("注册");
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        setLoginBtnStyle();
    }

    @Override
    public void initData() {
        dialog = new LoadingDialog(RegisterAc.this, 0);
    }

    @OnClick({R.id.get_code, R.id.clear_pwd, R.id.clear_confirm_pwd, R.id.complete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.get_code://获取验证码
                if (tel.getText().toString().trim().length() == 11) {
                    if (!Util.checkNet(getApplicationContext())) {
                        ToastUtil.showToast(getApplicationContext(), getString(R.string.error_net));
                    } else {
                        getCode();
                        NetWorkUtil.getRegsinCode(Util.md5(), tel.getText().toString(), "1", new HttpUtils.HttpCallback() {
                            @Override
                            public void onSuccess(String data) {
                                LogUtil.e("WDWDAWDAWD", data);
                                final CodeBean codeBean = MyGson.getInstance().fromJson(data, CodeBean.class);
                                ToastUtil.showToast(RegisterAc.this, codeBean.getMsg());
                            }

                            @Override
                            public void onError(String msg) {
                                super.onError(msg);
                            }
                        });
                    }


                } else {
                    ToastUtil.showToast(RegisterAc.this, "请输入正确的手机号");
                }

                break;
            case R.id.clear_pwd://清除密码
                pwd.setText("");
                break;
            case R.id.clear_confirm_pwd://清除确认密码
                pwdConfirm.setText("");
                break;
            case R.id.complete://完成
                commit();
                break;
        }
    }

    private void getCode() {
        new CountDownTimerHelp(this, get_code, 60000, 1000).start();
    }

    private void commit() {
        if (StringUtil.getStr(pwd).length() < 6 || StringUtil.getStr(pwd).length() > 20) {
            pwd_tip.setVisibility(View.VISIBLE);
        } else {
            commit2Net();
        }
    }

    private void commit2Net() {
        if (tel.getText().toString().trim().length() == 11) {
            if (code.getText().toString().length() > 0) {
                if (!Util.checkNet(getApplicationContext())) {
                    ToastUtil.showToast(getApplicationContext(), getString(R.string.error_net));
                } else {
                    if (pwd.getText().toString().equals(pwdConfirm.getText().toString())) {
                        dialog.show();
                        NetWorkUtil.register(Util.md5(), tel.getText().toString(), code.getText().toString(), pwd.getText().toString(), new HttpUtils.HttpCallback() {
                            @Override
                            public void onSuccess(String data) {
                                dialog.dismiss();
                                final CodeBean codeBean = MyGson.getInstance().fromJson(data, CodeBean.class);
                                finish();
                                ToastUtil.showToast(RegisterAc.this, codeBean.getMsg());
                            }

                            @Override
                            public void onError(String msg) {
                                super.onError(msg);
                                dialog.dismiss();
                            }
                        });
                    } else

                    {
                        ToastUtil.showToast(RegisterAc.this, "两次密码输入不一致");
                    }

                }
            } else {
                ToastUtil.showToast(RegisterAc.this, "请输入验证码");
            }
        } else {
            ToastUtil.showToast(RegisterAc.this, "请输入正确的手机号");
        }

    }

    private void setLoginBtnStyle() {
        pwdConfirm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!StringUtil.isEmpty(tel.getText().toString()) && s.length() > 0 && !StringUtil.isEmpty(code.getText().toString())
                        && !StringUtil.isEmpty(pwd.getText().toString()) && !StringUtil.isEmpty(pwdConfirm.getText().toString())) {
                    complete.setBackgroundResource(R.mipmap.login_red);
                } else {
                    complete.setBackgroundResource(R.mipmap.login_gray);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }
}

